package TeacherEvaluation.com.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import TeacherEvaluation.com.entity.Assess;
import TeacherEvaluation.com.entity.Course;
import TeacherEvaluation.com.entity.Department;
import TeacherEvaluation.com.entity.ResultMsg;
import TeacherEvaluation.com.entity.Student;
import TeacherEvaluation.com.service.classService;
import TeacherEvaluation.com.service.courseService;
import TeacherEvaluation.com.service.stu_assessService;
import TeacherEvaluation.com.service.teacherService;

@Controller
public class stu_assessController {
	@Autowired 
	stu_assessService service;
	@Autowired
	classService classService;
	@Autowired
	courseService courseService;
	@Autowired
	teacherService teacherService;
	
	@RequestMapping("/assAddIndex")
	public ModelAndView assAddIndex() {
		ModelAndView view = new ModelAndView("admin/teaching/addTeaching");
		view.addObject("classs", classService.selectClassNum());
		view.addObject("courses", courseService.selectCou());
		view.addObject("teachers", teacherService.selectTeacher());
		return view;
	}
	
	//添加院系
	@RequestMapping("/AssAdd")
	@ResponseBody
	public ResultMsg AssAdd(Assess Assess) {
		Course  course = courseService.selectCouByCoId(Assess.getAs_co_id());
		System.out.println(course.getTerm());
		String term = Assess.getAs_term()+course.getTerm();
		System.out.println(Assess.getAs_term());
		Assess.setAs_term(term);
		System.out.println(Assess.getAs_term());
		int i = service.insertAss(Assess);
		if (i==0) {
			return new ResultMsg(0, "授课已存在");
		}else {
			return new ResultMsg(1, "添加成功");
		}
	}
	/*
	//查看授课
	@RequestMapping("/AssSelect")
	@ResponseBody
	public ModelAndView AssSelect() {
		//创建视图
		ModelAndView mView = new ModelAndView("admin/teaching/showTeaching");
		
		//将查询结果发送给视图
		mView.addObject("itmes", service.selectAss());
		return mView;
	}*/
	
		//修改授课主页
		@RequestMapping("/AssUpdateIndex")
		public ModelAndView AssUpdateIndex(String as_id) {
			ModelAndView view = new ModelAndView("admin/teaching/updateTeaching");
			view.addObject("classs", classService.selectClassNum());
			view.addObject("courses", courseService.selectCou());
			view.addObject("teachers", teacherService.selectTeacher());
			return view;
		}
		
		//修改授课
		@RequestMapping("/AssUpdate")
		@ResponseBody
		public ResultMsg AssUpdate(Assess Assess) {
			Course  course = courseService.selectCouByCoId(Assess.getAs_co_id());
			String term = Assess.getAs_term()+course.getTerm();
			Assess.setAs_term(term);
			int i = service.insertAss(Assess);
			if (i==0) {
				return new ResultMsg(0, "授课已存在");
			}else {
				return new ResultMsg(1, "添加成功");
			}
		}
		
		//删除授课
		@RequestMapping("/AssDelete")
		@ResponseBody
		public ResultMsg AssDelete(String as_id) {
			int i = service.deleteAss(Integer.parseInt(as_id));
			if (i==0) {
				return new ResultMsg(0,"删除失败");
			}else {
				return new ResultMsg(1,"删除成功");
			}
		}
		
		//查看本期评教
		@RequestMapping("/assessTeacherTable")
		@ResponseBody
		public ModelAndView StuAssSelect(HttpServletRequest request) {
			//获取student
			HttpSession session=request.getSession();
			
			Student user=(Student)session.getAttribute("user");
			//创建视图
			ModelAndView mView = new ModelAndView("students/assessTeacherTable");
			//根据student的id查询授课
			mView.addObject("Assess",service.selectAssByStu(user.getId()));
			return mView;
		}
		
		//限制条件查询授课信息
		@RequestMapping("AssSelect")
		@ResponseBody
		public ModelAndView selectAssByTeacher(String className,String select_type) {
			List<Assess> Assess=service.selectAssByCondition(className,select_type);
			ModelAndView modelAndView = new ModelAndView("admin/teaching/showTeaching");
			modelAndView.addObject("itmes",Assess);
			return modelAndView;
		
			
		}
		
}
